package com.leetcode.August;



/**
 * @Description: 删除链表的倒数第 N 个结点
 * @Author: Mr.Fxy
 * @CreateTime: 2024-07-28 09:55
 */
public class removeEnd {
    public ListNode removeNthFromEnd(ListNode head, int n) {
//        int len=0;
//      while (head != null){
//          len++;
//          head=head.next;
//      }
//      int del=len-n;
//      while (del-- !=0){
//          head=head.next;
//      }
//      head.next=head.next.next;
//      return head;
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode first = dummy;
        ListNode second = dummy;
        for (int i = 0; i <= n; i++) {
            first = first.next;
        }
        while (first != null) {
            first = first.next;
            second = second.next;
        }
        second.next=second.next.next;
        return dummy.next;
    }
}
